import pytest
import os
import requests
from common_function import *
from selenium import webdriver
import selenium.webdriver.support.ui as ui

"""
1、conftest文件主要放置用例的前置/后置操作，例如连接数据库，后置清除测试数据等
"""


# 读取config下dbenv.yml 数据的方法
import yaml
@pytest.fixture(scope="session")
def dbinfo(request):
    dbfile = os.path.join(request.config.rootdir,
                        "config",
                        "dbenv.yml")
    print("dbinfo file path :%s" % dbfile)
    with open(dbfile, encoding="utf-8") as f:
        dbenv_config = yaml.load(f.read(), Loader=yaml.SafeLoader)
    print(dbenv_config)
    return dbenv_config


# 找目录层级，后期可以优化智能查找
@pytest.fixture(scope="session")
def dbinfo_common(request):
    dbfile = os.path.join(request.config.rootdir,
                        "case", "login_case",
                        "test_data.yml")  # 多路径拼接，一个双引号一个路径，从项目跟目录算起
    print("dbinfo file path :%s" % dbfile)
    with open(dbfile, encoding="utf-8") as f:
        dbenv_config = yaml.load(f.read(), Loader=yaml.SafeLoader)
    print(dbenv_config)
    return dbenv_config

# 读取目录中yml中的测试数据
@pytest.fixture(params=['dbinfo_common'])
def arg(request):
    print("logggggg:", request.getfixturevalue(request.param)['test_longin_data'])
    return request.getfixturevalue(request.param)


@pytest.fixture(scope="session")  # autouse=True 默认是False 需要手动开启
def login():
    # s = requests.sessions()  # 请求会话
    driver = chrome()

    # 以下也可以单独封装一个函数，放这也可以
    driver.get("https://dohko.login.internal.tiaofangzi.com/login?redirectURL=http%3A%2F%2Fpublic-mis.tiaofangzi.com")
    wait = ui.WebDriverWait(driver, 5)
    wait.until(lambda driver: driver.find_element_by_xpath('//*[@id="react-tabs-2"]/span').is_displayed())
    driver.find_element_by_xpath('//*[@id="react-tabs-2"]/span').click()
    userName = driver.find_element_by_xpath('//*[@id="login-section"]/div/form/div[1]/div/span/input[1]')
    userName.send_keys('13521653526')

    driver.find_element_by_xpath('//*[@id="login-section"]/div/form/div[2]/div/div/div[1]/span/input').click()
    password = driver.find_element_by_xpath('//*[@id="login-section"]/div/form/div[2]/div/div/div[1]/span/input')
    password.send_keys('000000')
    driver.find_element_by_id('loginBtn').click()

    yield driver
    driver.close()